package io.github.loan.back.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import io.github.loan.common.controller.BaseController;
import io.github.loan.common.util.ConstantFinalUtil;
import io.github.loan.common.util.PageInfoUtil;
import io.github.loan.investor.pojo.Bank;
import io.github.loan.investor.pojo.Grant;
import io.github.loan.investor.pojo.Investor;
import io.github.loan.investor.pojo.InvestorGrant;
import io.github.loan.investor.service.IInvestorService;

/**
 * 所有出资方相关的后台
 * @author Andone
 *
 */
@Controller
@RequestMapping("/back/investor/")
public class InvestorBackController extends BaseController {
	@Resource
	private IInvestorService investorService;
	
	/**
	 * 出资方列表
	 */
	@RequestMapping("/investorList.mvc")
	public String investorList(HttpServletRequest request){
		ConstantFinalUtil.LOGGER_SYSTEM.info("investorList");
		/*接收参数*/
		String keyword=request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		String coopType = request.getParameter("coopType");
		if (st == null) {
			st = "";
		}
		if (ed == null) {
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed)) {
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		condMap.put("coopType", coopType);
		List<Investor> investorList=this.investorService.findInvestorCondListService(pageInfoUtil, condMap);
		
		/*存储结果*/
		request.setAttribute("investorList", investorList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/*存储条件*/
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		request.setAttribute("coopType", coopType);
		return "/back/investor/investorList";
	}
	
	/**
	 * 出资方更新页面
	 */
	@RequestMapping("/investorUpdate.mvc")
	public String investorUpdate(String iid,HttpServletRequest request){
		ConstantFinalUtil.LOGGER_SYSTEM.info("---investorUpdate---");
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("id", iid);
		Investor investor=this.investorService.findOneInvestorService(condMap);
		request.setAttribute("investor", investor);
		return "/back/investor/investorUpdate";
	}
	
	/**
	 * 出资方更新提交
	 */
	@RequestMapping(value="/investorUpdateSubmit.mvc",produces="text/html;charset=UTF-8")
	public @ResponseBody String investorUpdateSubmit(String id,HttpServletRequest request,MultipartFile file){
		ConstantFinalUtil.LOGGER_SYSTEM.info("---investorUpdateSubmit---");
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("id",id);
		Investor investor=this.investorService.findOneInvestorService(condMap);
		if(investor!=null){
			/*接收参数*/
			String code=request.getParameter("code");
			String name = request.getParameter("name");
			String url = request.getParameter("url");
			String contactName = request.getParameter("contactName");
			String contactPhone = request.getParameter("contactPhone");
			String content = request.getParameter("content");
			String totalNum = request.getParameter("totalNum");
			String status = request.getParameter("status");
			String coopType = request.getParameter("coopType");
			String pubTime = request.getParameter("pubTime");
			
			investor.setPubTime(this.dateFormatUtil.parseDateTime(pubTime));
			investor.setCode(code);
			investor.setName(name);
			investor.setUrl(url);
			investor.setContactName(contactName);
			investor.setContactPhone(contactPhone);
			investor.setTotalNum(Integer.valueOf(totalNum));
			investor.setContent(content);
			investor.setCoopType(Byte.valueOf(coopType));
			investor.setStatus(Byte.valueOf(status));
			investor.setUpdateTime(new Date());
			/*处理文件上传需要的信息*/
			Map<String,Object> paramMap=new HashMap<String,Object>();
			/*上传文件*/
			paramMap.put("file", file);
			/*tomcat的真实路径*/
			paramMap.put("truePath", request.getRealPath("/"));
			JSONObject resultDbJSON = this.investorService.updateOneInvestorService(investor, paramMap);
			this.info = resultDbJSON.getString("info");
		}else{
			this.info="当前出资方不存在";
		}
		JSONObject resultJSON=this.returnJuiJSON(request, info);
		return resultJSON.toJSONString();
	}
	/**
	 * 授权列表
	 */
	@RequestMapping("/grantList.mvc")
	public String grantList(HttpServletRequest request){
		ConstantFinalUtil.LOGGER_SYSTEM.info("--grantList--");
		/*接收参数*/
		String keyword=request.getParameter("keyword");
		String status=request.getParameter("status");
		String st=request.getParameter("st");
		String ed=request.getParameter("ed");
		if (st == null) {
			st = "";
		}
		if (ed == null) {
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed)) {
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		PageInfoUtil pageInfoUtil=this.proccedPageInfo(request);
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		List<Grant> grantList=this.investorService.findGrantCondListService(pageInfoUtil, condMap);
		/* 存储结果 */
		request.setAttribute("grantList", grantList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/investor/grantList";
	}
	/**
	 * 银行列表
	 */
	@RequestMapping("/bankList.mvc")
	public String bankList(HttpServletRequest request){
		ConstantFinalUtil.LOGGER_SYSTEM.info("--bankList--");
		/*接收参数*/
		String keyword=request.getParameter("keyword");
		String status=request.getParameter("status");
		String st=request.getParameter("st");
		String ed=request.getParameter("ed");
		if (st == null) {
			st = "";
		}
		if (ed == null) {
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed)) {
			stDate = this.dateFormatUtil.parseDateTime(st);
			edDate = this.dateFormatUtil.parseDateTime(ed);
		}
		PageInfoUtil pageInfoUtil=this.proccedPageInfo(request);
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		List<Bank> bankList=this.investorService.findBankCondListService(pageInfoUtil, condMap);
		/* 存储结果 */
		request.setAttribute("bankList", bankList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/investor/bankList" ; 
	}
	/**
	 * 批量授权分类提交
	 */
	@RequestMapping(value="/cateBatch.mvc",produces="text/html;charset=UTF-8")
	public @ResponseBody String cateBatch(HttpServletRequest request){
		/* 往出资方授权分类表添加信息 */
		/* 接收参数 */
		/* 出资方ID */
		String iid=request.getParameter("iid");
		/* 获取所有的授权分类ID */
		String ids[]=request.getParameterValues("ids");
		
		int succeedCount=0;
		StringBuffer sb=new StringBuffer();
		/**
		 * 批量入库
		 */
		for(int i=0;i<ids.length;i++){
			String id=ids[i];
			InvestorGrant investorGrant=new InvestorGrant();
			investorGrant.setInvestorId(Integer.valueOf(iid));
			investorGrant.setCateGrantId(Integer.valueOf(id));
			investorGrant.setStatus(Byte.valueOf("1"));
			investorGrant.setCreateTime(new Date());
			investorGrant.setUpdateTime(new Date());
			/*发布时间，先传当前时间*/
			investorGrant.setPubTime(new Date());
			JSONObject resultJSON=this.investorService.insertOneInvestorGrantService(investorGrant);
			if("0".equalsIgnoreCase(resultJSON.get("code")+"")){
				succeedCount++;
			}else{
				sb.append(id+",");
			}
		}
		this.info="总条数:" + ids.length + ";成功:" + succeedCount + ";失败明细:" + sb; 
		JSONObject resultJSON=this.returnJuiJSON(request, info);
		return resultJSON.toJSONString();
	}
}
